package cn.iocoder.yudao.module.system.controller.admin.partnerapply;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserUpdateStatusReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.suggestion.SuggestionDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.partnerapply.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.partnerapply.PartnerApplyDO;
import cn.iocoder.yudao.module.system.service.partnerapply.PartnerApplyService;

@Tag(name = "管理后台 - 合伙人申请")
@RestController
@RequestMapping("/system/partnerapply")
@Validated
public class PartnerApplyController {

    @Resource
    private PartnerApplyService partnerApplyService;
    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建合伙人申请")
    @PreAuthorize("@ss.hasPermission('system:partnerapply:create')")
    public CommonResult<Long> createPartnerApply(@Valid @RequestBody PartnerApplySaveReqVO createReqVO) {
        return success(partnerApplyService.createPartnerApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合伙人申请")
    @PreAuthorize("@ss.hasPermission('system:partnerapply:update')")
    public CommonResult<Boolean> updatePartnerApply(@Valid @RequestBody PartnerApplySaveReqVO updateReqVO) {
        partnerApplyService.updatePartnerApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合伙人申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:partnerapply:delete')")
    public CommonResult<Boolean> deletePartnerApply(@RequestParam("id") Long id) {
        partnerApplyService.deletePartnerApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合伙人申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:partnerapply:query')")
    public CommonResult<PartnerApplyRespVO> getPartnerApply(@RequestParam("id") Long id) {
        PartnerApplyDO partnerApply = partnerApplyService.getPartnerApply(id);
        return success(BeanUtils.toBean(partnerApply, PartnerApplyRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改合伙人状态")
    @PreAuthorize("@ss.hasPermission('system:partnerapply:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        partnerApplyService.updateStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得合伙人申请分页")
    @PreAuthorize("@ss.hasPermission('system:partnerapply:query')")
    public CommonResult<PageResult<PartnerApplyRespVO>> getPartnerApplyPage(@Valid PartnerApplyPageReqVO pageReqVO) {
        PageResult<PartnerApplyDO> pageResult = partnerApplyService.getPartnerApplyPage(pageReqVO);
        PageResult<PartnerApplyRespVO> pageResulVO = BeanUtils.toBean(pageResult, PartnerApplyRespVO.class);
        if (CollUtil.isEmpty(pageResulVO.getList())) {
            return success(pageResulVO);
        }
        // 读取用户信息，进行拼接
        Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(convertSet(pageResult.getList(), PartnerApplyDO::getUserId));
        pageResulVO.getList().forEach(itemRespVO -> MapUtils.findAndThen(userMap, itemRespVO.getUserId(),
                userRespDTO -> itemRespVO.setNickname(userRespDTO.getNickname())));
        return success(pageResulVO);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合伙人申请 Excel")
    @PreAuthorize("@ss.hasPermission('system:partnerapply:export')")
    @OperateLog(type = EXPORT)
    public void exportPartnerApplyExcel(@Valid PartnerApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartnerApplyDO> list = partnerApplyService.getPartnerApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合伙人申请.xls", "数据", PartnerApplyRespVO.class,
                        BeanUtils.toBean(list, PartnerApplyRespVO.class));
    }

}